<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--添加产品结构-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">产品结构添加</h4>
            </div>
            <div class="modal-body">
                <form id="partForm" class="form-horizontal" role="form">
                    <input id="proId" type="hidden" th:value="${proJect.id}" name="proId">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="specialId" name="specialId">
                                <option value="" selected="selected">请选择</option>
                                <option th:each="r : ${specTypes}" th:value="${r.id}"
                                        th:text="${r.name}"
                                        th:selected="${r.id == specTypeId}"></option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">结构名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="proPart"
                                   placeholder="请输入产品结构名称" value="整体">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-save">保存</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>创建产品结构</h1>
    <h4>（如果某专业下的产品结构只有一个，则必须是整体；如果产品结构有多个，则可以自定义名称，但不能有整体；）</h4>
</section>
<section class="content">
    <form id="proStructForm" role="form"
          enctype="multipart/form-data">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">
                        <h4>项目信息</h4>
                        <div class="row">
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>项目编号：</label><label th:text="${proJect.proCode}"></label>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>项目名称：</label><label th:text="${proJect.proName}"></label>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>数量：</label><label th:text="${proJect.proNum}"></label>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>需方：</label><label th:text="${proJect.demander}"></label>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-inline">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label th:text="'分配科室：'+${proJect.depts}"></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <h4>产品结构</h4>
                        <div class="row" id="toolbar">
                            <div class="form-inline" th:if="${proStructStatus.status == 0}">
                                <button type="button" id="btn_add" shiro:hasPermission="dsn:proCreate:edit"
                                        class="btn btn-info">
                                    添加
                                </button>
                            </div>
                        </div>
                        <div class="row">
                            <table id="bootstrap-table"></table>
                        </div>
                        <div style="margin-top: 20px" th:if="${proStructStatus.status == 0}"
                             shiro:hasPermission="dsn:proCreate:commit">
                            <button type="button" id="btn_commit" class="btn btn-success pull-right"> 提交</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /** 打开编辑数据模态框 */
    function openEditProStruct(result) {
        $("#partForm").fillData(result);
        js.modal.open("modal-create");
        setTimeout(function () {
            js.validate.validing("partForm");
        }, 500);
    }

    /** 删除数据 */
    function del(id) {
        js.modal.confirm("确认删除吗？", function () {
            js.post({
                url: ctx + "designTask/proStruct/del",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    $(function () {

        /** 打开添加产品结构模态框 */
        $("#btn_add").on('click', function () {
            js.modal.open("modal-create")
        });

        /** 关闭添加产品结构模态框 */
        js.modal.hideEvent("modal-create", function () {
            var proId = $("#proId").val();
            var specialId = $("#specialId").val();
            js.validate.reset("partForm");
            js.reset("partForm");

            // 保留项目Id和类型
            $("#proId").val(proId);
            $("#specialId").val(specialId).trigger("change");
        });

        /** 产品结构表单校验 */
        js.validate.init("partForm", {
            fields: {
                specialId: {validators: {notEmpty: {message: '请选择类型'}}},
                proPart: {validators: {notEmpty: {message: '请输入产品结构名称'}}}
            }
        });

        /** 创建或编辑 */
        $("#btn-save").click(function () {
            var url = ctx + "designTask/proStruct/save";
            if ($("#id").val()) {
                url = ctx + "designTask/proStruct/update";
            }
            js.validSubmit({
                formId: "partForm",
                url: url,
                data: new FormData($("#partForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 产品结构列表 */
        table = js.table.init({
            url: ctx + "designTask/proStruct/proStructList",
            showExport: false,
            pagination: false,
            queryData: {proId: $("#proId").val()},
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "类型", field: 'special'},
                {title: "产品结构名称", field: 'proPart'},
                {title: "创建人", field: 'createName'},
                {title: "创建时间", field: 'createTime'},
                {
                    title: "提交状态", field: 'status',
                    formatter: function (value) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未提交</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-success">已提交</span>';
                        }
                    }
                },
                {
                    title: "操作",
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('dsn:proCreate:edit') && row.status === 1) {
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditProStruct(' + js.jsonStringify(row) + ')"> 编辑</a> ');
                        }
                        if (hasP('dsn:proCreate:del') && row.status === 1) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="del(' + row.id + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 提交产品结构 */
        $("#btn_commit").click(function () {

            var row = $('#bootstrap-table').bootstrapTable('getData');
            if (row.length == 0) {
                js.modal.warning("提交前请先创建产品结构！");
                return;
            }

            js.modal.confirm("确认提交吗？", function () {
                js.post({
                    url: ctx + "designTask/proStruct/subProStruct",
                    data: {proId: $("#proId").val()},
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                            $("#btn_commit").attr("style", "display:none;");
                            $("#btn_add").attr("style", "display:none;");
                        } else {
                            js.modal.warning(result.msg);
                        }
                    }
                })
            })
        })

    });

</script>
</body>
</html>
